package com.itheima.controller.backend;

import com.itheima.common.constant.MessageConstant;
import com.itheima.common.entity.QueryPageBean;
import com.itheima.common.entity.Result;
import com.itheima.pojo.OrderFindPageResult;
import com.itheima.pojo.OrderUpdateRequestBody;
import com.itheima.pojo.SubmitOrder;
import com.itheima.service.OrderService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.web.bind.annotation.*;

@RestController
@RequestMapping("/order")
@Slf4j
public class OrderController {

    @Autowired
    private OrderService orderService;
    @Autowired
    private RedisTemplate redisTemplate;

    @PostMapping("/findPage")
    public Result findPage(@RequestBody QueryPageBean queryPageBean) {

        log.info("分页查询预约信息:" + queryPageBean);


        Result result = new Result();

        result.setData(orderService.findPage(queryPageBean));

        result.setFlag(true);
        result.setMessage(MessageConstant.QUERY_ORDER_SUCCESS);

        return result;
    }

    @PostMapping("/update")
    public Result update(@RequestBody OrderUpdateRequestBody orderUpdateRequestBody) {

        log.info("更新预约信息:" + orderUpdateRequestBody);
        Result result = new Result();
        orderService.update(orderUpdateRequestBody);
        result.setFlag(true);
        result.setMessage(MessageConstant.ORDER_SUCCESS);
        return result;
    }
    /**
     * 根据预约id查询预约信息
     */
    @GetMapping("/findById")
    public Result findById(@RequestParam Integer id) {

        log.info("根据预约id查询预约信息:" + id);
        Result result = new Result();
        result.setFlag(true);
        result.setData(orderService.findById(id));
        result.setMessage(MessageConstant.QUERY_ORDER_SUCCESS);
        return result;
    }

    /**
     * 提交预约信息
     * @param submitOrder
     * @return
     */
    @PostMapping("/submitOrder")
    public Result submitOrder(@RequestBody SubmitOrder submitOrder) {
        log.info("提交预约信息:" + submitOrder);
        Integer telephone = submitOrder.getTelephone();
        if (telephone == null || telephone.toString().length() != 11){
            Result result = new Result();
            result.setFlag(false);
            result.setMessage("手机号不合法");
            return result;
        }
        if (submitOrder.getValidateCode() == null || submitOrder.getValidateCode().length() != 4){
            Result result = new Result();
            result.setFlag(false);
            result.setMessage("验证码不合法");
            return result;
        }
        String code = (String) redisTemplate.opsForValue().get("telephone:" + telephone);
        if (code == null || !code.equals(submitOrder.getValidateCode())){
            Result result = new Result();
            result.setFlag(false);
            result.setMessage("验证码错误");
            return result;
        }
        String validateCode = submitOrder.getValidateCode();
        if (!validateCode.equals(code)){
            Result result = new Result();
            result.setFlag(false);
            result.setMessage("验证码错误");
            return result;
        }
        try {
            Result result = orderService.submitOrder(submitOrder);
            return result;
        } catch (Exception e) {
            e.printStackTrace();
            Result result2 =  new Result();
            result2.setFlag(false);
            result2.setMessage("预约失败");
            return result2;
        }
    }
}
